package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.SearchDeviceParam;
import com.tvunetworks.center.common.model.dto.GeoDetail;
import com.tvunetworks.center.common.model.vo.FeatureInfoVo;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.PageDeviceGeoParam;
import com.tvunetworks.center.user.model.param.SaveDeviceGeoParam;
import com.tvunetworks.center.user.model.vo.DeviceGeoVo;
import com.tvunetworks.center.user.service.DeviceGeoService;
import com.tvunetworks.center.user.service.UserHttpService;
import com.tvunetworks.center.user.service.feign.DeviceFeignService;
import com.tvunetworks.center.user.service.feign.NewTpcFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: DeviceGeoController
 * @author lebronchen
 * @create: 2019-06-18 11:25
 **/


@RestController
@RequestMapping("/deviceGeo")
@Slf4j
public class DeviceGeoController {

    @Autowired
    private DeviceGeoService deviceGeoService;
    @Autowired
    private UserHttpService userHttpService;
    @Autowired
    private NewTpcFeignService  newTpcFeignService;
    @Autowired
    private DeviceFeignService deviceFeignService;

//    @GetMapping("/supportPage")
//    public Result<Object> supportPage(PageDeviceGeoParam param, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
//        PageInfo<DeviceGeoVo> deviceInfo = deviceGeoService.supportPage(param, pageNum, pageSize);
//        return ResultUtil.success(deviceInfo);
//    }

    /**
     *
     * @param loginUser 参数
     * @param param 参数
     * @param deviceGeoParam 参数
     * @param pageNum 参数
     * @param pageSize 参数
     * @return Result
     */
    @GetMapping("/page")
    public Result<Object> pageDeviceGeo(@Logined LoginUser loginUser, SearchDeviceParam param, PageDeviceGeoParam deviceGeoParam, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        param.setPageNum(pageNum);
        param.setPageSize(pageSize);
        return ResultUtil.success(deviceGeoService.pageDeviceGeo(loginUser, param, deviceGeoParam));
    }

    /**
     *
     * @param peerId peerId
     * @return Result
     */
    @GetMapping("/get")
    public Result<Object> getDeviceGeo(@RequestParam String peerId){
        DeviceGeoVo deviceGeo = deviceGeoService.getDeviceGeo(peerId);
        return ResultUtil.success(deviceGeo);
    }

    /**
     *
     * @param param param
     * @return  Result
     */
    @PostMapping("/save")
    public Result<Object> saveDeviceGeo(@Valid SaveDeviceGeoParam param){
        int result = deviceGeoService.saveDeviceGeo(param);
        if (result == 1) {
            return ResultUtil.error("81000701", "GeoService getLocation failed");
        } else if (result == 2) {
            return ResultUtil.paramError();
        }
        return ResultUtil.success();
    }

    /**
     * 通过设备查询设备位置
     * @param peerId peerId
     * @return Result
     */
    @GetMapping("/getGeoNameByDevice")
    public Result<String> getGeoNameByDevice(@RequestParam String peerId){
        return deviceGeoService.getGeoNameByDevice(peerId);
    }

    /**
     *
     * @param peerId peerId
     * @return Result
     */
    @GetMapping("/getGeoByDeviceIp")
    public Result<Object> getGeoByDeviceIp(@RequestParam String peerId){
        GeoDetail result = deviceGeoService.getGeoByDeviceIp(peerId);
        return ResultUtil.success(result);
    }

    /**
     *
     * @param peerId peerId
     * @param loginUser loginUser
     * @return Result
     */
    @GetMapping("/getFeatureListByPeerId")
    public Result<Object> getFeatureListByPeerId(@RequestParam String peerId,@Logined LoginUser loginUser){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        List<FeatureInfoVo>  featureInfoVoList = new ArrayList<>();
        try {
            Result<List<FeatureInfoVo>> featureList = newTpcFeignService.getFeatureListByPeerId(peerId);
            if(featureList !=null){
                featureInfoVoList =   featureList.getResult();
            }
        } catch (Exception e) {
            log.error("newTpcFeignService.getFeatureListByPeerId(peerId) Exception",e);
        }
        return ResultUtil.success(featureInfoVoList);
    }



}
